package com.yksj.healthtalk.ui.server;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.adapter.ShopListViewAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.ShopPayActivity;
import com.yksj.healthtalk.entity.ShopListItemEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.ui.R;

/**
 * 订单查询
 * @author Administrator
 *
 */
public class ServerCenterOrderDetailListActivity extends BaseFragmentActivity implements OnClickListener,OnRefreshListener2<ListView>, OnItemClickListener {
	
	
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private ShopListViewAdapter adapter;
	private int pagenum = 1;
	private int pagecount = 20;
	private List<ShopListItemEntity> entities = new ArrayList<ShopListItemEntity>();
	private String merchant_id;
	private String order_id;
	private JSONObject orderInfoMerchant;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.server_center_detail_list);
		
		initWidget();
		initData();
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		initTitle();
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.hall);
		mPullToRefreshListView.setOnRefreshListener(this);
		mListView = mPullToRefreshListView.getRefreshableView();
		mListView.setOnItemClickListener(this);
		findViewById(R.id.order).setOnClickListener(this);
		
		titleLeftBtn.setOnClickListener(this);
	}

	private void initData() {
		merchant_id = getIntent().getStringExtra("merchant_id");
		order_id = getIntent().getStringExtra("order_id");
		titleTextV.setText("订单详情");
		
		adapter = new ShopListViewAdapter(this,entities);
		mListView.setAdapter(adapter);
		
		if (merchant_id == null) {
			return;
		}
		dohttp(merchant_id);
	}

	private void dohttp(String merchant_id) {
		HttpRestClient.doHttpCarOrderDetailList(merchant_id, order_id, pagenum, pagecount,new JsonsfHttpResponseHandler(this){
			
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				super.onSuccess(statusCode, response);
			}
			
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				if (response.containsKey("goods")) {
					if (pagenum == 1) {
						entities.clear();
					}
					pagenum++;
					entities.addAll(JSONArray.parseArray(response.getJSONArray("goods").toString(), ShopListItemEntity.class)) ;
					adapter.notifyDataSetChanged();
				}
				if (response.containsKey("orderInfoMerchant")) {
					orderInfoMerchant = response.getJSONObject("orderInfoMerchant");
				}
				super.onSuccess(statusCode, response);
			}
			
			@Override
			public void onFinish() {
				mPullToRefreshListView.onRefreshComplete();
				super.onFinish();
			}
			
			
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.order:
			if (orderInfoMerchant != null) {
				StringBuilder builder = new StringBuilder();
				builder.append("sec=")
						.append(orderInfoMerchant.getString("sec"))
						.append("&")
						.append("userid=")
						.append(orderInfoMerchant.getString("userid"))
						.append("&")
						.append("orderId=")
						.append(orderInfoMerchant.getString("orderId"))
						;
				Intent intent = new Intent(getApplicationContext(), ShopPayActivity.class);
				intent.putExtra("url",builder.toString());
				intent.putExtra("addr", orderInfoMerchant.getString("url"));
				startActivity(intent);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		pagenum = 1;
		dohttp(merchant_id);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		dohttp(merchant_id);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.setClass(getApplicationContext(), ServerCenterDescription.class);
		intent.putExtra("entity", entities.get(arg2 - 1));
		startActivity(intent);
	}

}
